$(function () {
 let type = 1;
 getList();
 // "买入","卖出"的切换
 $(".mai_title_list").click(function () {
  $(this).addClass("mai_title_active");
  $(this).siblings().removeClass("mai_title_active");
  type = $(this).attr("data-type"); // 1-买入 2-卖出
  getList()
 });


 function getList() {
  let data = {
   uid: localStorage.getItem("u_id"),
   token: localStorage.getItem("token"),
   language_type: localStorage.getItem("language_type"),
   d_type: type
  }
  $.ajax({
   type: 'post',
   url: baseURL + '/index.php?m=user&c=deal_list',
   data: data,
   success: function success(data) {
    data = JSON.parse(data);
    is_login(data);
    if (data.code == 1) {
     let detailsList = '';
     if (data.data.length == 0) {
      detailsList = `<div class="if_null">
        <img class="if_null_img" src="./icons/nothing.png" alt="">
        <div class="if_null_text col_000 f30" data-ch="暂无内容" data-en="No data">暂无内容</div>
       </div>`
     } else {
      data.data.forEach(function (item) {
       detailsList += `<div class="mai_details latout_mask padding_032 border_radius03">
         <div class="flex_be">
          <div class="flex_st">
           <span class=" col_fff" data-ch="恐龙种类:" data-en="Dino type: ">恐龙种类:</span>
           <span>${item.d_name}</span>
          </div>
          <div class="flex_st">
           <span class=" col_fff" data-ch="捕猎时间:" data-en="Hunt time: ">捕猎时间:</span class=" col_fff">
           <span class="mai_details_time">${item.d_s_date}</span>
          </div>
         </div>
         <div class="flex_be">
          <div class="flex_st">
           <span class=" col_fff" data-ch="价格:" data-en="Price: ">价格:</span class=" col_fff">
           <span>${item.d_price}</span>
          </div>
          <div class="flex_st">
           <span class=" col_fff" data-ch="付款时间:" data-en="Payment time:">付款时间:</span class=" col_fff">
           <span class="mai_details_time">${item.d_f_date}</span class="mai_details_time">
          </div>
         </div>
         <div class="text_center mai_jilu_ID">
          <span class=" col_fff" data-ch="交易ID:" data-en="Reference ID: ">交易ID:</span class=" col_fff">
          <span>${item.d_num}</span>
         </div>
         <div class="text_center f24 col_fff" data-ch="请以此为交易ID" data-en="Please use this as Reference ID">请以此为交易ID</div>
         <div class="mai_btn_box flex_ar">
          <div data-id="${item.id}" data-status="${item.d_m_status}" class="mai_btn_list border_radius05 mai_btn_list1 col_fff ${item.d_status_info == '收益中' || item.d_status_info == '已挂卖' ? 'jui_none' : ''}">${item.d_status_info}</div>
          <a href="mai_xijie.html?id=${item.id}&&type=${type}" class="mai_btn_list col_fff border_radius05 mai_btn_list2" data-ch="细节"
           data-en="Details">细节</a>
          <a href="shensu.html?id=${item.id}" style="background:red" class="mai_btn_list col_fff border_radius05 mai_btn_list3 ${item.d_status_info == '收益中' || item.d_status_info == '已挂卖' ? 'jui_none' : ''}" data-ch="申诉"
           data-en="Appeal">申诉</a>
         </div>
        </div>`

      })
     }
     $("#mai_list").html(detailsList);
     language();

     var d_id; // 该订单的id
     var d_status;
     $(".mai_btn_list1").click(function () { // 买卖方第一个按钮的操作
      d_id = $(this).attr("data-id");
      d_status = $(this).attr('data-status'); // 按钮的状态 买家:(0-立即付款 1-待收款) 卖家:(0-待付款 1-确认收款)
      if ((type == 1 && d_status == 0) || (type == 2 && d_status == 1)) {
       $(".mai_pass_box").removeClass("display_none");
       // 点击弹窗的"确认",弹窗消失,先掉密码接口,密码正确后,再掉确认付款接口
       $(".mai_pass_btn").click(function () {
        $(".mai_pass_box").addClass("display_none");
        var data2 = { // 交易密码接口,判断密码是否正确
         uid: localStorage.getItem("u_id"),
         token: localStorage.getItem("token"),
         language_type: localStorage.getItem("language_type"),
         pay_pwd: $(".mai_pass").val()
        };
        $.ajax({
         type: 'post',
         url: baseURL + '/index.php?m=user&c=checking_pay_pwd',
         data: data2,
         success: function success(data) {
          data = JSON.parse(data);
          is_login(data);
          $(".mai_pass").val("");
          if (data.code == 1) { // 密码正确,再掉确认付款接口
           var data_info = {
            uid: localStorage.getItem("u_id"),
            token: localStorage.getItem("token"),
            language_type: localStorage.getItem("language_type"),
            d_id: d_id
           };
           if (type == 1) {
            $.ajax({
             type: 'post',
             url: baseURL + '/index.php?m=user&c=confirm_payment',
             data: data_info,
             success: function success(data) {
              data = JSON.parse(data);
              is_login(data);
              box_timer(data.msg);
              if (data.code == 1) {
               //getList();
               setTimeout(function () {
                window.location.reload();
               }, 1000);
              }
             }
            })
           } else {
            $.ajax({
             type: 'post',
             url: baseURL + '/index.php?m=user&c=confirm_receipt',
             data: data_info,
             success: function success(data) {
              data = JSON.parse(data);
              is_login(data);
              box_timer(data.msg);
              if (data.code == 1) {
               setTimeout(function () {
                //getList();
                window.location.reload();
               }, 1500);
              }
             }
            })
           }
          } else {
           box_timer(data.msg);
          }
         }
        });
       })
      }
     });

     
    } else {
     box_timer(data.msg)
    }
   }
  })
 }


 language();
 documentName()
});